package kr.hkit.myframework.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionFactory {
	private String driver; //com.mysql.jdbc.DIRVER
	private String url;
	private String user;
	private String password;
	private static ConnectionFactory instance = new ConnectionFactory();
	
	
	public static Connection getConnection() {
		return instance.createConnection(); //외부에서 이것만 보임 public~~
	}
	
	
	private ConnectionFactory() { //생성자..
		loadProperties();
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			System.err.printf("%s", "MySQL Driver not Found");
			System.exit(-1); //-1은 비정상 종료. 0이 정상종료 
		}
	}

	
	private void loadProperties() {
		Properties prop = DBProperties.loadPropertis("dbcon.xml");
		driver = prop.getProperty("DRIVER");
		url = prop.getProperty("URL");
		user = prop.getProperty("USER");
		password = prop.getProperty("PASSWORD");
		
	}

	private Connection createConnection(){ //java.sql을 import
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, user, password); //연결시도
			System.out.println("MySQL Connection Success");
		} catch (SQLException e) {
			System.err.printf("%s", "ERROR : Unable Connect to MySQL");
		} 
		return con;
	}
	
}



